package cn.tedu.boot2_3.Controller;

import cn.tedu.boot2_3.entity.Drop;
import cn.tedu.boot2_3.entity.Hero;
import cn.tedu.boot2_3.utils.DBUtils;
import com.alibaba.druid.support.spring.stat.annotation.Stat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.*;

@Controller
public class HeroController {
    @RequestMapping("/add")
    @ResponseBody
    public String add(Hero hero){
        try (Connection conn= DBUtils.getConn()){
            String sql="insert into hero value(null,?,?,?)";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,hero.getName());
            ps.setString(2,hero.getType());
            ps.setInt(3,hero.getMoney());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加成功";
    }
    @RequestMapping("/select")
    @ResponseBody
    public String select(){
        String html="<table border=\"1\">\n" +
                "    <caption>英雄列表页面</caption>\n" +
                "    <tr>\n" +
                "        <th>id</th>\n" +
                "        <th>name</th>\n" +
                "        <th>type</th>\n" +
                "        <th>money</th>\n" +
                "    </tr>";
        try (Connection conn=DBUtils.getConn();){
            String sql="select *from hero";
            Statement s=conn.createStatement();
            ResultSet rs=s.executeQuery(sql);
            while (rs.next()){
                int id=rs.getInt(1);
                String name=rs.getString(2);
                String type=rs.getString(3);
                int money=rs.getInt(4);
                html+="<tr>";
                html+="<td>"+id+"</td>";
                html+="<td>"+name+"</td>";
                html+="<td>"+type+"</td>";
                html+="<td>"+money+"</td>";
                html+="</tr>";
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/drop")
    @ResponseBody
    public String drop(Drop drop){
        try (Connection conn=DBUtils.getConn();){
            String sql="delete from hero where name=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,drop.getDrop());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "删除成功";
    }
}








